#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author: tangzhichao

from typing import List
from .base import BaseSlicer
from langchain.text_splitter import RecursiveCharacterTextSplitter


class HybridSlicer(BaseSlicer):
    """混合切片策略(语义+字符)"""

    def __init__(self, chunk_size: int = 500, overlap: int = 50):
        self.char_splitter = RecursiveCharacterTextSplitter(
            chunk_size=chunk_size,
            chunk_overlap=overlap,
            separators=["\n\n", "\n", "。", "！", "？", "；", " ", ""]
        )

    def split(self, text: str) -> List[str]:
        # 先按语义分割，再对长段落进行字符分割
        chunks = []
        for paragraph in text.split("\n\n"):
            if len(paragraph) > 1000:  # 长段落使用字符分割
                chunks.extend(self.char_splitter.split_text(paragraph))
            else:
                chunks.append(paragraph)
        return chunks

    @property
    def strategy_name(self) -> str:
        return "hybrid"